package vip.zhenzicheng.design_patterns.singleton.lazy;

/**
 * @author zhenzicheng
 * @date: 2022-05-24 23:17
 */
public class SingletonWithCheckAndSyncLock {
  private static SingletonWithCheckAndSyncLock INSTANCE;

  private SingletonWithCheckAndSyncLock() {
  }

  public static SingletonWithCheckAndSyncLock getInstance() {
    if (INSTANCE == null) {
      synchronized (SingletonWithCheckAndSyncLock.class) { // 如果两个线程都在这一行争抢只有一个线程成功，当释放锁以后由于第二个线程已经通过非空判断，所以还是会重新创建
        INSTANCE = new SingletonWithCheckAndSyncLock();
      }
    }
    return INSTANCE;
  }
}
